package com.ge.mytestparent.dto;

import io.swagger.v3.oas.annotations.media.Schema;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author geqingxiang
 * @PackageName myProject
 * @Package com.example.myproject.utils
 * @Date 2023/4/18 15:27
 * @Version 1.0
 */
@Schema(name = "返回参数说明")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1084094914898698049L;
    private static final String DEFAULT_ERROR_CODE = "999999";
    private static final String DEFAULT_SUCCESS_CODE = "100000";
    @Schema(name = "请求id")
    private String requestId;
    @Schema(name = "请求编码，code为0代表的是成功，其他都是失败！")
    private String code;
    @Schema(name = "返回的提示信息")
    private String msg;
    @Schema(name = "true 代表的是返回成功，false代表的是返回失败！")
    private boolean success;
    private T data;
    @Schema(name = "请求的时间戳")
    private String timestamp;

    private Result() {
    }

    private Result(String requestId, String code, String msg, T data) {
        this.requestId = requestId;
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.timestamp = getStringDate();
    }

    public static <T> Result<T> error() {
        return error("999999", "未知异常，请联系管理员");
    }

    public static <T> Result<T> error(String msg) {
        return error("999999", msg);
    }

    public static <T> Result<T> error(String code, String msg) {
        String requestId = UUID.randomUUID().toString();
        Result result = new Result(requestId, code, msg, (Object) null);
        result.setSuccess(false);
        return result;
    }

    public static <T> Result<T> error(String code, T data, String msg) {
        String requestId = UUID.randomUUID().toString();
        String timestamp = getStringDate();
        Result result = new Result(requestId, code, msg, data);
        result.setSuccess(false);
        return result;
    }

    public static <T> Result<T> success() {
        return (Result<T>) success((Object) null, "操作成功！");
    }

    public static <T> Result<T> success(T data) {
        return success(data, "操作成功！");
    }

    /**
     * @deprecated
     */
    public static <T> Result<T> success(String msg) {
        return (Result<T>) success((Object) null, msg);
    }

    public static <T> Result<T> success(T data, String msg) {
        String requestId = UUID.randomUUID().toString();
        String timestamp = getStringDate();
        Result result = new Result(requestId, "100000", msg, data);
        result.setSuccess(true);
        return result;
    }

    public String getRequestId() {
        return this.requestId;
    }

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public T getData() {
        return this.data;
    }

    public String getTimestamp() {
        return this.timestamp;
    }

    public void setRequestId(final String requestId) {
        this.requestId = requestId;
    }

    public void setCode(final String code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setSuccess(final boolean success) {
        this.success = success;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public void setTimestamp(final String timestamp) {
        this.timestamp = timestamp;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Result)) {
            return false;
        } else {
            Result<?> other = (Result) o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label75:
                {
                    Object this$requestId = this.getRequestId();
                    Object other$requestId = other.getRequestId();
                    if (this$requestId == null) {
                        if (other$requestId == null) {
                            break label75;
                        }
                    } else if (this$requestId.equals(other$requestId)) {
                        break label75;
                    }

                    return false;
                }

                Object this$code = this.getCode();
                Object other$code = other.getCode();
                if (this$code == null) {
                    if (other$code != null) {
                        return false;
                    }
                } else if (!this$code.equals(other$code)) {
                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                if (this.isSuccess() != other.isSuccess()) {
                    return false;
                } else {
                    Object this$data = this.getData();
                    Object other$data = other.getData();
                    if (this$data == null) {
                        if (other$data != null) {
                            return false;
                        }
                    } else if (!this$data.equals(other$data)) {
                        return false;
                    }

                    Object this$timestamp = this.getTimestamp();
                    Object other$timestamp = other.getTimestamp();
                    if (this$timestamp == null) {
                        if (other$timestamp != null) {
                            return false;
                        }
                    } else if (!this$timestamp.equals(other$timestamp)) {
                        return false;
                    }

                    return true;
                }
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Result;
    }

    public int hashCode() {
        int result = 1;
        Object $requestId = this.getRequestId();
        result = result * 59 + ($requestId == null ? 43 : $requestId.hashCode());
        Object $code = this.getCode();
        result = result * 59 + ($code == null ? 43 : $code.hashCode());
        Object $msg = this.getMsg();
        result = result * 59 + ($msg == null ? 43 : $msg.hashCode());
        result = result * 59 + (this.isSuccess() ? 79 : 97);
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        Object $timestamp = this.getTimestamp();
        result = result * 59 + ($timestamp == null ? 43 : $timestamp.hashCode());
        return result;
    }

    public String toString() {
        return "Result(requestId=" + this.getRequestId() + ", code=" + this.getCode() + ", msg=" + this.getMsg() + ", success=" + this.isSuccess() + ", data=" + this.getData() + ", timestamp=" + this.getTimestamp() + ")";
    }

    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
}
